package be.bonamis.umpire.ui.client.activities;

import be.bonamis.projects.umpire.business.actions.GetAllQuizA;
import be.bonamis.projects.umpire.business.actions.GetAllQuizR;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireA;
import be.bonamis.projects.umpire.business.actions.GetGamesByUmpireR;
import be.bonamis.projects.umpire.business.actions.GetLoginR;
import be.bonamis.projects.umpire.business.actions.GetMonthsA;
import be.bonamis.projects.umpire.business.actions.GetMonthsR;
import be.bonamis.projects.umpire.business.actions.GetUmpireByIdA;
import be.bonamis.projects.umpire.business.actions.GetUmpiresA;
import be.bonamis.projects.umpire.business.actions.GetUmpiresR;
import be.bonamis.umpire.ui.client.ClientFactory;
import be.bonamis.umpire.ui.client.mvp.WhoView;
import be.bonamis.umpire.ui.client.places.BootPlace;
import be.bonamis.umpire.ui.client.places.DispoPlace;
import be.bonamis.umpire.ui.client.places.HistoryPlace;
import be.bonamis.umpire.ui.client.places.QuizPlace;
import be.bonamis.umpire.ui.client.places.WhoPlace;
import be.credoc.common.gwt.actions.client.ActionServiceWrapper;
import be.credoc.common.gwt.actions.client.OnResponseCommand;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class WhoActivity extends BaseActivity<WhoPlace> implements WhoView.Presenter {

	private WhoView whoView;
	private ActionServiceWrapper asw;

	public WhoActivity(WhoPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
		this.clientFactory = clientFactory;
		this.asw = clientFactory.getActionServiceWrapper();
		this.place = place;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		whoView = clientFactory.getWhoView();
		if (place.getUmpire() != null) {
			whoView.setUmpire(place.getUmpire());
		}
		whoView.setPresenter(this);
		
		panel.setWidget(whoView.asWidget());
	}

	@Override
	public void searchUmpires(String text) {
		GetUmpiresA<GetUmpiresR> getUmpires = new GetUmpiresA<GetUmpiresR>();
		getUmpires.setName(text);
		asw.execute(getUmpires, new OnResponseCommand<GetUmpiresR>() {
			@Override
			public void execute() {
				whoView.updateUmpireList(response.getUmpiresDTOList());
			}
		});

	}
	
	@Override
	public void getUmpireById(long id) {
		GetUmpireByIdA<GetLoginR> action = new GetUmpireByIdA<GetLoginR>();
		action.setId(id);
		asw.execute(action, new OnResponseCommand<GetLoginR>() {
			@Override
			public void execute() {
				whoView.setValues(response.getUmpireDTO());
			}
		});

	}

	@Override
	public void dispoButtonClicked() {
		GetMonthsA<GetMonthsR> action = new GetMonthsA<GetMonthsR>();
		asw.execute(action, new OnResponseCommand<GetMonthsR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new DispoPlace("dispo", whoView.getUmpire(), response.getSetDates()));
			}
		});
	}

	@Override
	public void whoButtonClicked() {
		whoView.setValues(whoView.getUmpire());
		clientFactory.getPlaceController().goTo(new WhoPlace("who", whoView.getUmpire()));
	}

	@Override
	public void quizButtonClicked() {
		GetAllQuizA<GetAllQuizR> action = new GetAllQuizA<GetAllQuizR>();
		asw.execute(action, new OnResponseCommand<GetAllQuizR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new QuizPlace("quiz", whoView.getUmpire(), response.getQuizs()));
			}
		});
	}

	@Override
	public void historyButtonClicked() {
		GetGamesByUmpireA<GetGamesByUmpireR> action = new GetGamesByUmpireA<GetGamesByUmpireR>();
		action.setId(whoView.getUmpire().getId());
		asw.execute(action, new OnResponseCommand<GetGamesByUmpireR>() {
			@Override
			public void execute() {
				clientFactory.getPlaceController().goTo(
						new HistoryPlace("history", whoView.getUmpire(), response.getGames()));
			}
		});
	}

	@Override
	public void homeButtonClicked() {		
		clientFactory.getPlaceController().goTo(new BootPlace("menu", place.getUmpire()));		
	}
}
